package com.ibeeking.found.common.config.secret;

import com.ibeeking.found.common.entity.JksEntity;
import com.ibeeking.found.common.config.prop.EncryptProp;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.encryption.EncryptUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;

import javax.annotation.Resource;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
import java.security.PrivateKey;
import java.security.PublicKey;


/**
 * @Author ibeeking
 */
@Configuration
public class EncryptConfig {

    private static final String ENV_DEV = "dev";
    private static final String ENV_SIT = "sit";

    @Resource
    private EncryptProp encryptProp;

    @Value("${spring.profiles.active:dev}")
    private String env;

    @Bean
    public RsaConfig rsaConfigBean() {
        PrivateKey privateKey = null;
        PublicKey publicKey = null;
        try {
            JksEntity jks = encryptProp.getJks();
            KeyStore keyStore = KeyStore.getInstance("JKS");
            InputStream is = null;
            if (ENV_DEV.equalsIgnoreCase(env)) {
                is = new ClassPathResource(jks.getPath()).getInputStream();
            } else {
                if (ENV_SIT.equalsIgnoreCase(env) && !new File(jks.getPath()).exists()) {
                    is = new ClassPathResource("jks/mall.jks").getInputStream();
                } else {
                    is = new FileInputStream(jks.getPath());
                }
            }
            keyStore.load(is, EncryptUtils.AesEncrypt.decrypt(jks.getStorePass(), encryptProp.getSecretKey()).toCharArray());
            privateKey = (PrivateKey) keyStore.getKey(jks.getAlias(), EncryptUtils.AesEncrypt.decrypt(jks.getKeyPass(), encryptProp.getSecretKey()).toCharArray());
            publicKey = keyStore.getCertificate(jks.getAlias()).getPublicKey();
        } catch (Exception e) {
            throw new BusinessException("读取加密文件异常:" + e);
        }
        return new RsaConfig(publicKey, privateKey, encryptProp.getSecretKey());
    }

    public static void main(String[] args) {
        String key = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDjEyIRAK/5TOb671+4hxxStcJ0h+JK/d8IeJSRG+t6j7UzYoJ9g8BDqj1XgP4KvicuQXdny7qns+qCrfAe78gsk2DEqpcZrdIJ+i+nAvLMyN8CJPbh9wDX5jP3i+qKtHuGHlI1dMFQljH/hO9EN+yObPHy6lonu5YVtseo/abIzwIDAQAB";
        String key1 = "MIICeAIBADANBgkqhkiG9w0BAQEFAASCAmIwggJeAgEAAoGBAOMTIhEAr/lM5vrvX7iHHFK1wnSH4kr93wh4lJEb63qPtTNign2DwEOqPVeA/gq+Jy5Bd2fLuqez6oKt8B7vyCyTYMSqlxmt0gn6L6cC8szI3wIk9uH3ANfmM/eL6oq0e4YeUjV0wVCWMf+E70Q37I5s8fLqWie7lhW2x6j9psjPAgMBAAECgYEAo53HsCSkp6eHz0vn1thTyiz5XmoC3jIrILlxo1VQyVD/KXVTvQjP4nGqHYDui+MT7DpzxTCA4BG9akfWexQfUNOif+Zlk0yNEOoo4ZTu+ea8cP9+iRBrHEcxo1WXlk9qdOktnprLyV9mPAZsOvA3BhopD6WL1CJnUPt9b6hBExECQQD184yjenBVZnA4B25cZBUBrUIZCFxpxJrfcFA3oMAzFuKeVs7Z87EhK5zqqkFZsMML2b8GvEKVXhsm1sAzw/w7AkEA7FomS6vb2T9uetCxJKxeCX5oTKRH3TU6/ITGkgUYFpdeVpU2Wo9STpc7Yc/RsgS5YqOBxxUZJfCS8qirx7PgfQJBANhi5Voy78AC9zokN5FrsBugjmWtuHiJs+RfUQQQFukb+ldqcmTJBQ706wJ+ti4t2PyFH47SdRYrDGJYUBN7G18CQQCzMNnt7xscR3okhQp4qnIVMpAy4y5U6OGSx7ugw1ny7sooTLJL0HNvgDsWOjBajjhbL2naHQXW/OD8NqMeIiMlAkAHDjFCsTeRQWQFElsNXfSvTghc7CaHMrODAkZGgCgcXkPJskKAwY3DyGNRnTIw5diG2YLzSSL31BeACg3u3ico";
        String encrypt = EncryptUtils.RsaEncrypt.encrypt("895348", EncryptUtils.RsaEncrypt.getPublicKey(key));

//        String storePass = EncryptUtils.AesEncrypt.decrypt("ZrlV+Q0fHfJPVet/HueZPWVJvQsSd9W42LEUJp9RCO4=", "AegeanplaceOutLets");
//        String keyPass = EncryptUtils.AesEncrypt.decrypt("ZrlV+Q0fHfJPVet/HueZPWVJvQsSd9W42LEUJp9RCO4=", "AegeanplaceOutLets");
//        String st = EncryptUtils.AesEncrypt.encrypt("Abcd@1234", "ibeeking");
        System.out.println(encrypt);
        System.out.println(EncryptUtils.RsaEncrypt.decrypt(encrypt, EncryptUtils.RsaEncrypt.getPrivateKey(key1)));
        System.out.println("test");
    }
}
